//
//  LVSessionListCell.m
//  LEVE
//
//  Created by 上官惠阳 on 2017/3/10.
//  Copyright © 2017年 dashuju. All rights reserved.
//

#import "LVSessionListCell.h"
#import "LVSessionModel.h"
#import "UIView+Layer.h"
#import "NSString+Size.h"
#import <objc/runtime.h>

@interface LVSessionListCell ()
@property (copy,nonatomic)void (^longPressBlock)(LVSessionModel *model);
@end

@interface LVSessionListCell ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *unReadCountLabelWidth;

@end
@implementation LVSessionListCell
{
    LVSessionModel *_model;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];

    [_headIconBtn makeRoundedCorners];
    [_unReadCountLabel makeRoundedCorners];
}
- (void)loadCellWithModel:(LVSessionModel *)model
{
    _model = model;
    [self loadUnreadLabel:model.unReadCount];
}
- (void)loadUnreadLabel:(NSString *)unReadCount
{
    if (![unReadCount intValue]) {
        _unReadCountLabel.hidden = YES;
        return;
    }else{
        _unReadCountLabel.hidden = NO;
    }
    _unReadCountLabel.text = unReadCount;
    CGFloat width = [unReadCount widthWithFont:[UIFont systemFontOfSize:12] constrainedToHeight:15] + 7;
    _unReadCountLabelWidth.constant = width > 15 ? width : 15;
}
- (void)setLastNewsType:(LVLastNewsType)lastNewsType
{
    _lastNewsType = lastNewsType;

    switch (_lastNewsType) {
        case LVLastNewsFailed:
            [_stateBtn setImage:[UIImage imageNamed:@"im_home_fail"] forState:UIControlStateNormal];
            break;
        case LVLastNewsReaded:
            [_stateBtn setImage:[UIImage imageNamed:@"im_home_read"] forState:UIControlStateNormal];
            break;
        case LVLastNewsWaitingToSend:
            [_stateBtn setImage:[UIImage imageNamed:@"im_home_send"] forState:UIControlStateNormal];
            break;
        case LVLastNewsHasReceived:
            [_stateBtn setImage:nil forState:UIControlStateNormal];
            break;
        case LVLastNewsHasSend:
            [_stateBtn setImage:[UIImage imageNamed:@"im_home_wait"] forState:UIControlStateNormal];
            break;
        case LVLastNewsUnread:
            [_stateBtn setImage:nil forState:UIControlStateNormal];
            break;

        default:
            break;
    }
}
- (void)addLongPressWithBlock:(void (^)(LVSessionModel *model))longPreBlock
{
    if (!self.gestureRecognizers) {
        _longPressBlock = longPreBlock;
        UILongPressGestureRecognizer *longPre = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
        [self addGestureRecognizer:longPre];
    }
}
- (void)longPressAction:(UILongPressGestureRecognizer *)longPre
{
    if (_longPressBlock) {
        //这里后面要传数据model回去比较好
        _longPressBlock(_model);
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
@end
